﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Tank
{
    public partial class HraciaPlocha : Form
    {
        string st = "";
        Random rnd = new Random();
        private static int player_live = 9;
        private static int bot_live = 15;
        public int tank_sizeX;
        public int tank_sizeY;
        Form f = new Form();
        PictureBox tnk_pic1 = null;
        PictureBox tnk_bot_pic = null;
        PictureBox tnk_bot2_pic = null;
        Tank tnk1 = null;
        TankBot tnk_bot = null;
        TankBot tnk_bot2 = null;
        Bitmap img = null;

        Bitmap tnk_img_dopredu = new Bitmap(100, 100);
        Bitmap tnk_img_dozadu = new Bitmap(100, 100);
        Bitmap tnk_img_dolava = new Bitmap(100, 100);
        Bitmap tnk_img_doprava = new Bitmap(100, 100);
        Bitmap tnk2_img_dopredu = new Bitmap(100, 100);
        Bitmap tnk2_img_dozadu = new Bitmap(100, 100);
        Bitmap tnk2_img_dolava = new Bitmap(100, 100);
        Bitmap tnk2_img_doprava = new Bitmap(100, 100);
        Bitmap cesta = new Bitmap(100, 100);
        Bitmap prekazka = new Bitmap(100, 100);
        Bitmap map_img;
        Bitmap naboj = new Bitmap(5, 5);
        bool dolava, doprava, dopredu, dozadu;
        private static int DOPREDU = 0;
        private static int DOZADU = 1;
        private static int DOLAVA = 2;
        private static int DOPRAVA = 3;
        public static int SIRKA;
        public static int VYSKA;
        public static int aaa = 0;
        Button ct = new Button();
        Button bk = new Button();
        Button ex = new Button();

        public HraciaPlocha()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            UpdateStyles();

            Init();
        }

        private void Init()
        {
            SetScreen();
            NacitajMapu();
            PridatTank();
            MapImg();
            TankImg();
            tnk_pic1.Image = tnk_img_dopredu;
            KresliMapu();
            VypisInfo();
            dolava = false;
            doprava = false;
            dolava = false;
            doprava = false;
            AddEscForm();



            TankMove.Start();
            ShotMove.Start();

        }

        private void AddEscForm()
        {
            f.AutoSize = false;
            f.Height = 150;
            f.Width = 50;
            f.StartPosition = FormStartPosition.Manual;
            f.Opacity = 0.9;
            f.Left = (int)(0.8 * Width / 2 - 50);
            f.Top = Height / 2 + 75;
            f.FormBorderStyle = FormBorderStyle.None;
            ct.Text = "&Continue";
            bk.Text = "&Back To The Menu";
            ex.Text = "&Exit";
            ct.Parent = f;
            bk.Parent = f;
            ex.Parent = f;
            ct.Top = 10;
            ct.Left = 20;
            bk.Top = 50;
            bk.Left = 20;
            ex.Top = 90;
            ex.Left = 20;

            ex.Click += new System.EventHandler(Quit);
            bk.Click += new System.EventHandler(Back);
            ct.Click += new System.EventHandler(Continue);

        }

        private void Hracia_Plocha_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Space:
                    {
                        tnk1.shot();
                        break;
                    }
                case Keys.Left:
                    {
                        dopredu = false;
                        dozadu = false;
                        dolava = true;
                        doprava = false;
                        break;
                    }
                case Keys.Right:
                    {
                        dopredu = false;
                        dozadu = false;
                        dolava = false;
                        doprava = true;
                        break;
                    }
                case Keys.Up:
                    {
                        dopredu = true;
                        dozadu = false;
                        dolava = false;
                        doprava = false;
                        break;
                    }
                case Keys.Down:
                    {
                        dopredu = false;
                        dozadu = true;
                        dolava = false;
                        doprava = false;
                        break;
                    }
            }
            if (e.KeyCode == Keys.Escape)
            {
                f.Show();
                TankMove.Stop();
                Cursor.Show();
            }

        }
        private void Back(object sender, EventArgs e)
        {
            System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(menu));
            th.Start();
            this.Close();
            GC.Collect();
        }
        private void menu()
        {
            Application.Run(new Menu());
        }

        private void Continue(object sender, EventArgs e)
        {
            f.Hide();
            TankMove.Start();
            Cursor.Hide();
        }

        private void Quit(object sender, EventArgs e)
        {

            Application.Exit();
        }

        private void NasmerujBot()
        {
            switch (tnk_bot.smer)
            {
                case 0: tnk_bot_pic.Image = tnk2_img_dopredu;
                    break;
                case 1: tnk_bot_pic.Image = tnk2_img_dozadu;
                    break;
                case 2: tnk_bot_pic.Image = tnk2_img_dolava;
                    break;
                case 3: tnk_bot_pic.Image = tnk2_img_doprava;
                    break;
            }

            switch (tnk_bot2.smer)
            {
                case 0: tnk_bot2_pic.Image = tnk2_img_dopredu;
                    break;
                case 1: tnk_bot2_pic.Image = tnk2_img_dozadu;
                    break;
                case 2: tnk_bot2_pic.Image = tnk2_img_dolava;
                    break;
                case 3: tnk_bot2_pic.Image = tnk2_img_doprava;
                    break;
            }

        }

        private void HraciaPlocha_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up: dopredu = false;
                    break;
                case Keys.Down: dozadu = false;
                    break;
                case Keys.Left: dolava = false;
                    break;
                case Keys.Right: doprava = false;
                    break;
            }
        }

        private void TankImg()
        {
            //SetStyle(ControlStyles.Opaque, true);

            //tnk_pic1.BackColor = Color.Transparent;
            //tnk_bot_pic.BackColor = Color.Transparent;
            //tnk_bot2_pic.BackColor = Color.Transparent;

            try
            {
                naboj = new Bitmap(@"images\naboj.dat");
                img = new Bitmap(@"images\tnk.gif");
                tnk_img_dopredu = img.Clone(Rectangle.FromLTRB(0, 0, 100, 100), System.Drawing.Imaging.PixelFormat.Undefined);
                tnk_img_dozadu = img.Clone(Rectangle.FromLTRB(101, 0, 200, 100), System.Drawing.Imaging.PixelFormat.Undefined); 
                tnk_img_dolava = img.Clone(Rectangle.FromLTRB(201, 0, 300, 100), System.Drawing.Imaging.PixelFormat.Undefined);
                tnk_img_doprava = img.Clone(Rectangle.FromLTRB(301, 0, 400, 100), System.Drawing.Imaging.PixelFormat.Undefined);

                tnk2_img_dopredu = img.Clone(Rectangle.FromLTRB(0, 100, 100, 200), System.Drawing.Imaging.PixelFormat.Undefined);
                tnk2_img_dozadu = img.Clone(Rectangle.FromLTRB(101, 100, 200, 200), System.Drawing.Imaging.PixelFormat.Undefined);
                tnk2_img_dolava = img.Clone(Rectangle.FromLTRB(201, 100, 300, 200), System.Drawing.Imaging.PixelFormat.Undefined);
                tnk2_img_doprava = img.Clone(Rectangle.FromLTRB(301, 100, 400, 200), System.Drawing.Imaging.PixelFormat.Undefined);

                //st = img.GetPixel(0, 0).ToArgb().ToString();



                //for (int i = 0; i < 400; i++)
                //{
                //    for (int j = 0; j < 200; j++)
                //    {
                //        Color c = img.GetPixel(i, j);
                        
                //        if (c.ToArgb() == 16777215)
                //        {
                //            c = Color.FromArgb(255, 255, 255, 255);
                //        }

                //        if (j / 100 == 1)
                //        {
                //            switch (i / 100)
                //            {
                //                case 0: tnk_img_dopredu.SetPixel(i % 100, j % 100, c);
                //                    break;
                //                case 1: tnk_img_dozadu.SetPixel(i % 100, j % 100, c);
                //                    break;
                //                case 2: tnk_img_dolava.SetPixel(i % 100, j % 100, c);
                //                    break;
                //                case 3: tnk_img_doprava.SetPixel(i % 100, j % 100, c);
                //                    break;
                //            }
                //        }
                //        else
                //        {
                //            switch (i / 100)
                //            {
                //                case 0: tnk2_img_dopredu.SetPixel(i % 100, j % 100, c);
                //                    break;
                //                case 1: tnk2_img_dozadu.SetPixel(i % 100, j % 100, c);
                //                    break;
                //                case 2: tnk2_img_dolava.SetPixel(i % 100, j % 100, c);
                //                    break;
                //                case 3: tnk2_img_doprava.SetPixel(i % 100, j % 100, c);
                //                    break;
                //            }
                //        }
                //    }
                //}

                img.Dispose();

                tnk_img_dolava = (Bitmap)tnk_img_dolava.GetThumbnailImage(Tank.sizeX, Tank.sizeY, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);
                tnk_img_doprava = (Bitmap)tnk_img_doprava.GetThumbnailImage(Tank.sizeX, Tank.sizeY, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);
                tnk_img_dopredu = (Bitmap)tnk_img_dopredu.GetThumbnailImage(Tank.sizeX, Tank.sizeY, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);
                tnk_img_dozadu = (Bitmap)tnk_img_dozadu.GetThumbnailImage(Tank.sizeX, Tank.sizeY, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);

                tnk2_img_dolava = (Bitmap)tnk2_img_dolava.GetThumbnailImage(Tank.sizeX, Tank.sizeY, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);
                tnk2_img_doprava = (Bitmap)tnk2_img_doprava.GetThumbnailImage(Tank.sizeX, Tank.sizeY, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);
                tnk2_img_dopredu = (Bitmap)tnk2_img_dopredu.GetThumbnailImage(Tank.sizeX, Tank.sizeY, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);
                tnk2_img_dozadu = (Bitmap)tnk2_img_dozadu.GetThumbnailImage(Tank.sizeX, Tank.sizeY, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);

            }
            catch (Exception)
            {
                Hlasenie();
            }
        }

        private void MapImg()
        {
            naboj.SetPixel(3, 3, Color.Blue);

            try
            {
                img = new Bitmap(@"Map\Map.map");
                for (int i = 0; i < 400; i++)
                {
                    for (int j = 0; j < 100; j++)
                    {
                        Color c = img.GetPixel(i, j);
                        if (c == Color.White)
                        {
                            c = Color.Transparent;
                        }
                        switch (i / 100)
                        {
                            case 0: cesta.SetPixel(i % 100, j % 100, c);
                                break;
                            case 1: prekazka.SetPixel(i % 100, j % 100, c);
                                break;
                        }
                    }
                }
                cesta = (Bitmap)cesta.GetThumbnailImage(Map.sirka, Map.vyska, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);
                prekazka = (Bitmap)prekazka.GetThumbnailImage(Map.sirka, Map.vyska, new Bitmap.GetThumbnailImageAbort(ThmbCallback), IntPtr.Zero);

                img.Dispose();
            }
            catch (Exception)
            {
                Hlasenie();
            }
        }

        private bool ThmbCallback()
        {
            return true;
        }

        private void Hlasenie()
        {
            Label error = new Label();
            error.Text = "Application error, please reinstall it";
            error.Font = new Font(error.Font.FontFamily, 20);
            error.AutoSize = true;
            error.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - 300;
            error.Top = Screen.PrimaryScreen.WorkingArea.Height / 2;
            error.Parent = this;
        }

        private void SetScreen()
        {

            VYSKA = Height = Screen.PrimaryScreen.WorkingArea.Size.Height;
            SIRKA = Width = Screen.PrimaryScreen.WorkingArea.Size.Width;

            Left = 0;
            Top = 0;
            Cursor.Hide();
        }

        private void PridatTank()
        {
            tnk1 = new Tank();
            tnk_pic1 = new PictureBox();
            tnk_pic1.BackColor = Color.Transparent;
            tnk_pic1.Height = Tank.sizeY;
            tnk_pic1.Width = Tank.sizeX;
            tnk_pic1.Image = tnk_img_dopredu;
            tnk_pic1.Parent = this;
            tnk_pic1.BringToFront();
            tank_sizeX = Tank.sizeX;
            tank_sizeY = Tank.sizeY;
            tnk1.team = 0;

            tnk_bot = new TankBot(5, 0);
            tnk_bot_pic = new PictureBox();
            tnk_bot_pic.BackColor = Color.Transparent;
            tnk_bot_pic.Height = Tank.sizeY;
            tnk_bot_pic.Width = Tank.sizeX;
            tnk_bot_pic.Image = tnk2_img_dozadu;
            tnk_bot_pic.Parent = this;
            tnk_bot_pic.BringToFront();
            tnk_bot.team = 1;

            tnk_bot2 = new TankBot(2, 0);
            tnk_bot2_pic = new PictureBox();
            tnk_bot2_pic.BackColor = Color.Transparent;
            tnk_bot2_pic.Height = Tank.sizeY;
            tnk_bot2_pic.Width = Tank.sizeX;
            tnk_bot2_pic.Image = tnk2_img_dozadu;
            tnk_bot2_pic.Parent = this;
            tnk_bot2_pic.BringToFront();
            tnk_bot2.team = 1;
        }

        private void NacitajMapu()
        {
            try
            {
                Map.Nacitaj();
            }
            catch (Exception)
            {
                MessageBox.Show("File not found");
                Application.Exit();
                TankMove.Stop();
            }
        }

        private void KresliMapu()
        {
            map_img = new Bitmap(10 * Map.sirka, 10 * Map.vyska);

            for (int i = 0; i < Map.x; i++)
            {
                for (int j = 0; j < Map.y; j++)
                {
                    switch (Map.map[i, j])
                    {
                        case 'X':
                            {
                                for (int k = 0; k < Map.sirka; k++)
                                {
                                    for (int l = 0; l < Map.vyska; l++)
                                    {
                                        Color c = cesta.GetPixel(k, l);
                                        map_img.SetPixel(Map.sirka * j + k, Map.vyska * i + l, c);
                                    }
                                }
                                break;

                            }

                        case 'P':
                            {
                                for (int k = 0; k < Map.sirka; k++)
                                {
                                    for (int l = 0; l < Map.vyska; l++)
                                    {
                                        Color c = prekazka.GetPixel(k, l);
                                        map_img.SetPixel(Map.sirka * j + k, Map.vyska * i + l, c);
                                    }
                                }
                                break;
                            }

                    }
                }
            }
            BackgroundImage = map_img;

            label1.Left = 10 * Map.sirka;
            label1.Top = 10;
        }

        private void VypisInfo()
        {
            label1.Text = "Lives:\n\n";
            label1.Text += "Player: " + player_live + "\n\n\nPC: " + bot_live;
            label1.Text += '\n' + st;
            //label1.Text += "\n\n" + tnk_bot.str + "\n" + tnk_bot2.str;
        }

        private void changeMap(int x, int y, char to)
        {

            for (int k = 0; k < Map.sirka; k++)
            {
                for (int l = 0; l < Map.vyska; l++)
                {
                    Color c = cesta.GetPixel(k, l);
                    map_img.SetPixel(Map.sirka * x + k, Map.vyska * y + l, c);
                }
            }

        }

        private void TankMove_Tick(object sender, EventArgs e)
        {
            if (dolava == true)
            {
                tnk1.Move(DOLAVA);
                tnk_pic1.Image = tnk_img_dolava;
            }

            else if (dozadu == true)
            {
                tnk1.Move(DOZADU);
                tnk_pic1.Image = tnk_img_dozadu;
            }
            else if (doprava == true)
            {
                tnk1.Move(DOPRAVA);
                tnk_pic1.Image = tnk_img_doprava;
            }

            else if (dopredu == true)
            {
                tnk1.Move(DOPREDU);
                tnk_pic1.Image = tnk_img_dopredu;
            }
            tnk1.share();

            NasmerujBot();

            tnk_pic1.Left = tnk1.x;
            tnk_pic1.Top = tnk1.y;
            tnk_bot_pic.Left = tnk_bot.x;
            tnk_bot_pic.Top = tnk_bot.y;
            tnk_bot2_pic.Left = tnk_bot2.x;
            tnk_bot2_pic.Top = tnk_bot2.y;

            Share.player.x = tnk1.x;
            Share.player.y = tnk1.y;
            Share.bot1.x = tnk_bot.x;
            Share.bot1.y = tnk_bot.y;
            Share.bot2.x = tnk_bot2.x;
            Share.bot2.y = tnk_bot2.y;

            tnk_bot.Krok();
            tnk_bot2.Krok();
        }

        private void ShotMove_Tick(object sender, EventArgs e)
        {
            tnk1.ShotMove();
            tnk_bot.ShotMove();
            tnk_bot2.ShotMove();

            if (Share.reset_player)
            {
                player_live--;

                if (player_live < 1)
                {
                    GameOver();
                }
                else
                {
                    int x = rnd.Next(9);
                    int y = rnd.Next(9);
                    while (Map.map[x, y] == 'P' && (tnk_bot.x != x || tnk_bot.y != y) && (tnk_bot2.x != x || tnk_bot2.y != y))
                    {
                        x = rnd.Next(9);
                        y = rnd.Next(9);
                    }
                    tnk1 = new Tank(y, x);

                    tnk1.team = 0;
                }

                Share.reset_player = false;
            }

            if (Share.reset_bot1)
            {
                bot_live--;
                if (bot_live < 1)
                {
                    YouWin();
                }
                else
                {


                    int x = rnd.Next(9);
                    int y = rnd.Next(9);
                    while (Map.map[x, y] == 'P' && (tnk1.x != x || tnk1.y != y))
                    {
                        x = rnd.Next(9);
                        y = rnd.Next(9);
                    }
                    tnk_bot = new TankBot(y, x);
                    tnk_bot.team = 1;
                }

                Share.reset_bot1 = false;
            }

            if (Share.reset_bot2)
            {
                bot_live--;
                if (bot_live < 1)
                {
                    YouWin();
                }
                else
                {


                    int x = rnd.Next(9);
                    int y = rnd.Next(9);
                    while (Map.map[x, y] == 'P' && (tnk1.x != x || tnk1.y != y))
                    {
                        x = rnd.Next(9);
                        y = rnd.Next(9);
                    }
                    tnk_bot2 = new TankBot(y, x);
                    tnk_bot2.team = 1;
                }

                Share.reset_bot2 = false;
            }

            VypisInfo();
            Refresh();
        }

        private void YouWin()
        {
            TankMove.Stop();
            ShotMove.Stop();
            label2.Left = (int)(0.8 * Width / 2 - 100);
            label2.Top = Height / 2 - 20;
            label2.Text = "You Win";
            label2.Visible = true;
            tnk_bot_pic.Dispose();
            tnk_bot2_pic.Dispose();
            ct.Enabled = false;
            f.Show();
            Cursor.Show();
        }

        private void GameOver()
        {
            TankMove.Stop();
            ShotMove.Stop();
            label2.Left = (int)(0.8 * Width / 2 - 100);
            label2.Top = Height / 2 - 20;
            label2.Text = "Game Over";
            label2.Visible = true;
            tnk_pic1.Dispose();
            ct.Enabled = false;
            f.Show();
            Cursor.Show();
        }

        private void HraciaPlocha_Paint(object sender, PaintEventArgs e)
        {
            //st = "";
            Strela s1 = tnk1.s;
            Strela s2 = tnk_bot.s;
            Strela s3 = tnk_bot2.s;

            while (s1 != null)
            {
                //st += tnk1.s.x + " " + tnk1.s.y + "\n";
                e.Graphics.DrawImage(naboj, s1.x, s1.y);
                s1 = s1.next;
            }

            while (s2 != null)
            {
                //st += tnk_bot.s.x + " " + tnk_bot.s.y + "\n";
                e.Graphics.DrawImage(naboj, s2.x, s2.y);
                s2 = s2.next;
            }
            while (s3 != null)
            {
                //st += tnk_bot2.s.x + " " + tnk_bot2.s.y + "\n";
                e.Graphics.DrawImage(naboj, s3.x, s3.y);
                s3 = s3.next;
            }
        }


    }
}